package com.fept.msgcore.msgserver.protocol.util.crypto;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;

/**
 * Created by Srma on 2/9/2017.
 */
public class AESUtil {
    private static final byte[] IV_PARAMETER = new byte[]{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15};

    public AESUtil() {
    }

    public static String encryptBase64(String strKey, String strIn) throws InvalidKeyException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidAlgorithmParameterException, IllegalBlockSizeException, BadPaddingException {
        return Base64Util.encode(encrypt(strKey, strIn.getBytes()));
    }

    public static byte[] encrypt(byte[] key, byte[] data) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, InvalidAlgorithmParameterException, IllegalBlockSizeException, BadPaddingException {
        SecretKeySpec skeySpec = getKey(key);
        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
        IvParameterSpec iv = new IvParameterSpec(IV_PARAMETER);
        cipher.init(1, skeySpec, iv);
        return cipher.doFinal(data);
    }

    public static byte[] encrypt(String strKey, byte[] strIn) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, InvalidAlgorithmParameterException, IllegalBlockSizeException, BadPaddingException {
        return encrypt(strKey.getBytes(), strIn);
    }

    public static byte[] decryptBase64(String strKey, String base64Str) throws InvalidKeyException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidAlgorithmParameterException, IllegalBlockSizeException, BadPaddingException {
        return decrypt(strKey, Base64Util.decode(base64Str));
    }

    public static byte[] decrypt(byte[] key, byte[] data) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, InvalidAlgorithmParameterException, IllegalBlockSizeException, BadPaddingException {
        SecretKeySpec skeySpec = getKey(key);
        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
        IvParameterSpec iv = new IvParameterSpec(IV_PARAMETER);
        cipher.init(2, skeySpec, iv);
        return cipher.doFinal(data);
    }

    public static byte[] decrypt(String strKey, byte[] strIn) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, InvalidAlgorithmParameterException, IllegalBlockSizeException, BadPaddingException {
        return decrypt(strKey.getBytes(), strIn);
    }

    private static SecretKeySpec getKey(byte[] arrBTmp) {
        byte[] arrB = new byte[16];

        for(int skeySpec = 0; skeySpec < arrBTmp.length && skeySpec < arrB.length; ++skeySpec) {
            arrB[skeySpec] = arrBTmp[skeySpec];
        }

        SecretKeySpec var3 = new SecretKeySpec(arrB, "AES");
        return var3;
    }
}
